package com.qf.web;

import com.alibaba.fastjson.JSON;
import com.qf.model.*;
import com.qf.model.pojo.MyPro;
import com.qf.service.AccountService;
import com.qf.service.ProductService;
import com.qf.service.UserService;
import com.qf.service.impl.AccountServiceImpl;
import com.qf.service.impl.ProductServiceImpl;
import com.qf.service.impl.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/product")
public class ProductServlet extends BascServlet {

    private ProductService productService = new ProductServiceImpl();
    private UserService userService = new UserServiceImpl();
    private AccountService accountService = new AccountServiceImpl();


    //查询所有理财产品
    public void finAll(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        List<Product> all = productService.findAll();
        Massage<List<Product>> m = new Massage<>(1, "查询成功", all);
        //转换成json格式
        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);
    }

    //根据p_id查询理财产品信息
    public void finaByPid(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String p = req.getParameter("p_id");
        Massage<Product> m = null;
        if (p == "") {
            m = new Massage<>(-1, "理财产品编号不可为空");
        } else {
            //查询数据
            Product byPid = productService.findByPid(Integer.parseInt(p));
            if (byPid != null) {
                m = new Massage<>(1, "查询成功", byPid);
            } else {
                m = new Massage<>(-1, "理财产品不存在");
            }
        }
        //转换成json格式
        String s = JSON.toJSONString(m);
        //返回数据
        resp.getWriter().write(s);
    }

    //购买理财商品
    public void buyProduct(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int uid = Integer.parseInt(req.getParameter("u_id"));        //用户id
        String cid = req.getParameter("c_id");                      //银行卡号id  如果-1   未选择付款方式，1：app余额付款  其余：使用银行卡号付款
        int pid = Integer.parseInt(req.getParameter("p_id"));        //理财产品id
        int num = Integer.parseInt(req.getParameter("num"));         //购买份额
        String pwd = req.getParameter("pwd");                       //支付密码 app付款时输入的需要时app支付密码  银行卡付款时需要的是银行卡支付密码

        Massage m = null;

        //根据用户id查询用户信息
        User user = userService.findByUid(uid);
        //获取要购买的理财产品信息
        Product product = productService.findByPid(pid);

        //计算需要付款总价
        double zprice = num * product.getP_price();

        //判断用户付款方式，并查询余额是否充足
        if (cid.equals("-1")) {
            //未选择付款方式
            m = new Massage(-1, "请选择付款方式");
        } else if (cid.equals("1")) {
            if (user.getU_cpwd().equals(pwd)) {
                //app余额支付
                //验证余额是否充足
                if (user.getU_balance() > zprice) {
                    //验证

                    //余额充足，可以购买
                    //1.app扣款
                    //2.添加我的理财产品
                    //3.添加消费记录


                    //user ：用户对象
                    //card ：银行卡对象 （使用用户app余额付款时可以为空）
                    //product ：理财产品对象
                    //money：需要扣款金额
                    //num ：份额
                    //p：   1：app扣款  2：表示：银行卡扣款
                    //double money, int num, int p, User user, Card card,Product product
                    int i = productService.cardBuyProduct(zprice, num, 1, user, null, product);
                    //判断是否购买成功
                    if (i == 1) {
                        m = new Massage(1, "购买成功");
                    } else {
                        m = new Massage(-1, "购买失败，请重试");
                    }
                } else {
                    m = new Massage(-1, "app余额不足，请更换支付方式");
                }
            } else {
                m = new Massage(-1, "app支付密码不正确");
            }

        } else {
            //使用银行卡号付款
            //根据银行卡号查询银行卡信息
            Card card = accountService.findCardByCid(cid);
            if (card.getC_pwd().equals(pwd)) {
                //验证银行卡余额是否充足
                if (card.getC_balance() > zprice) {
                    //余额充足，可以购买
                    //1.银行卡扣款
                    //2.添加我的理财产品
                    //3.添加消费记录
                    //user ：用户对象
                    //card ：银行卡对象 （使用用户app余额付款时可以为空）
                    //product ：理财产品对象
                    //money：需要扣款金额
                    //num ：份额
                    //p：   1：app扣款  2：表示：银行卡扣款
                    //double money, int num, int p, User user, Card card,Product product
                    int i = productService.cardBuyProduct(zprice, num, 2, user, card, product);
                    //判断是否购买成功
                    if (i == 1) {
                        m = new Massage(1, "购买成功");
                    } else {
                        m = new Massage(-1, "购买失败，请重试");
                    }
                } else {
                    m = new Massage(-1, "银行卡余额不足，请更换支付方式");
                }
            } else {
                m = new Massage(-1, "银行卡支付密码不正确");
            }
        }


        //转换成json
        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);
    }

    //查询当前用户id的所有已购买的理财产品信息
    public void findMyProByUid(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String uid = req.getParameter("uid");
        Massage<List<MyPro>> m = null;
        if (uid == "" || uid == null) {
            m = new Massage<>(-1, "请登录后查看");
        } else {
            //查询理财产品信息
            int u_id = Integer.parseInt(uid);
            List<MyPro> list = productService.findMyProByUid(u_id);
            m = new Massage<>(1, "查询成功", list);
        }
        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);
    }


    //查询所有已审核的银行卡
    public void findCardByUser(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String u_id = req.getParameter("uid");
        Massage<List<Card>> m = null;
        if (u_id != "") {
            int uid = Integer.parseInt(u_id);
            //根据用户id查询用户银行卡
            List<Card> list = productService.findCardByUid(uid);
            m = new Massage<>(1, "查询成功", list);
        } else {
            m = new Massage<>(-1, "请先登录");
        }
        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);
    }

    //卖出以购买的理财产品
    public void sellMyPro(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int uid = Integer.parseInt(req.getParameter("uid"));            //用户id
        int id = Integer.parseInt(req.getParameter("id"));              //我的理财产品id
        String cid = req.getParameter("cid");                           //银行卡号
        double rate = Double.parseDouble(req.getParameter("rate"));     //利息
        System.out.println(rate);
        //根据id查询出我的理财产品信息
        MyProduct myProById = productService.findMyProById(id);
        //根据cid擦汗寻出对应的银行卡信息
        Card card = accountService.findCardByCid(cid);
        //根据用户id查询用户信息
        User user = userService.findByUid(uid);

        //执行卖出理财产品
        int i = productService.sellProduct(rate, user, card, myProById);
        Massage m = null;
        if (i == 1) {
            m = new Massage(1, "卖出成功");
        } else {
            m = new Massage(-1, "卖出失败，请重试");
        }
        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);
    }

}
